/**
 * ---------------------------
 * Time: 2017/5/17 22:27.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
var RunningTrack = Main.createConfigClass({
    default: {
        defaultVar: function () {
            return {
                centrePoint: null
            }
        },
        defaultConfig: function () {
            return {
                speed: 2,
                acceleration: 0,
                direction: 0,
                maxSpeed: 5,
                centrePoint: {
                    x: 0,
                    y: 0
                }
            }
        },
        onConfigured: function () {
            var config = this.config;
            this.speed = config.speed;
            this.acceleration = config.acceleration;
            this.direction = config.direction;
            this.maxSpeed = config.maxSpeed;
            this.centrePoint = new Cielar.Point(config.centrePoint);
            if (config.doChanges && typeof config.doChanges == 'function') {
                this.doChanges = config.doChanges;
            }
            if (config.onRunningInterceptor && typeof config.onRunningInterceptor == 'function') {
                this.onRunningInterceptor = config.onRunningInterceptor;
            }
            if (config.doRunningOnce && typeof config.doRunningOnce == 'function') {
                this.doRunningOnce = config.doRunningOnce;
            }
        }
    },
    defaultPrototype: {
        runningStart: function () {
            var _this = this;
            this.moveHandle = setInterval(function () {
                _this.doRunning();
            }, 16);
        },
        runningEnd: function () {
            if (!this.moveHandle) {
                return;
            }
            clearInterval(this.moveHandle);
        },
        doChanges: function () {

        },
        doRunningOnce: function () {

        },
        onRunningInterceptor: function (e) {

        },
        /**
         * 每次运动调用该方法
         */
        doRunning: function () {
            // console.log(this);
            this.doChanges();
            this.speed = this.speed + this.acceleration;
            if (this.speed > this.maxSpeed) {
                this.speed = this.maxSpeed;
            }
            var distance = this.speed;
            var centrePoint = this.centrePoint.getOtherPointFromDistanceAndDeg(distance, this.direction);
            var event = {
                centrePoint: centrePoint,
                preventRunning: false
            };
            this.onRunningInterceptor(event);
            if (event.preventRunning) {
                return;
            }
            this.centrePoint = centrePoint;
            this.doRunningOnce();
        },
        /**
         * 调用该方法，会返回当前状态的坐标及运动角度
         * @returns {Cielar.Point|*}
         */
        getCurrentPoint: function () {
            return this.centrePoint.extend({
                runningTrack:{
                    deg: this.direction
                }
            });
        }
    }
});